﻿using System;
using System.Collections.Generic;
using NAnt.Core;
using NAnt.Core.Attributes;
using System.IO;
using NAnt.SVN.Util;
using NAnt.SVN.Tasks;
using NAnt.SVN.UtilTasks.Constants;
using NAnt.Core.Types;

namespace NAnt.SVN.UtilTasks
{
    /// <summary>
    /// Use this task to write a nant file receipt to uninstall all the files
    /// $Id$
    /// </summary>
    [TaskName("writereceipt")]
    public sealed class WriteUninstallReceipt : Task
    {
        #region Private Fields

        FileInfo tofile;
        FileSet installedfiles;
        string oldbase = string.Empty;
        string newbase = string.Empty;
        string projectname = string.Empty;

        #endregion

        #region Public fields

        /// <summary>
        /// The file to write the receipt to
        /// </summary>
        [TaskAttribute("tofile", Required = true)]
        public FileInfo ToFile
        {
            get { return tofile; }
            set { tofile = value; }
        }

        /// <summary>
        /// The fileset to base the receipt on
        /// </summary>
        [BuildElement("fileset",Required=true)]
        public FileSet InstalledFiles
        {
            get { return installedfiles; }
            set { installedfiles = value; }
        }

        /// <summary>
        /// Set this property to look for a base directory in the 
        /// filenames and repleace it with the new base
        /// </summary>
        [TaskAttribute("oldbasedir", Required = true)]
        [StringValidator(AllowEmpty = false)]
        public string OldBase
        {
            get { return oldbase; }
            set { oldbase = value; }
        }

        /// <summary>
        /// Set the new basedirectory in the receipt
        /// </summary>
        [TaskAttribute("newbasedir",Required=true)]
        [StringValidator(AllowEmpty=false)]
        public string NewBase
        {
            get { return newbase; }
            set { newbase = value; }
        }

        /// <summary>
        /// The projectname, only informative
        /// </summary>
        [TaskAttribute("project", Required = true)]
        [StringValidator(AllowEmpty = false)]
        public string ProjectName
        {
            get { return projectname; }
            set { projectname = value; }
        }


        #endregion

        /// <summary>
        /// Main exec function
        /// </summary>
        protected override void ExecuteTask()
        {
            try
            {
                Log(Level.Info, Resources.Strings.UTIL0108, ToFile.FullName);
                TextWriter writer = new StreamWriter(ToFile.FullName);
                writer.WriteLine(@"<?xml version='1.0'?>");
                writer.WriteLine(string.Format(
                        @"<project name='{0}' default='{1}' basedir='{2}'>",
                        Resources.Strings.UTIL0105,
                        Resources.Strings.UTIL0106,
                        NewBase
                    ));
                writer.WriteLine(string.Format("\t<description>{0}</description>",
                        string.Format(Resources.Strings.UTIL0107, ProjectName)
                    ));
                writer.WriteLine(string.Format("t<target name='{0}'>", Resources.Strings.UTIL0106));
                foreach (string f in InstalledFiles.FileNames)
                {
                    writer.WriteLine(string.Format("\t\t<delete verbose='true' file='{0}' />",
                        f.Replace(OldBase, ".")));
                }
                writer.WriteLine("\t</target>");
                writer.WriteLine(@"</project>");
                //Loop to the files and write it
                //Flush 'n close
                writer.Flush();
                writer.Close();
            }
            catch (Exception e)
            {
                throw new BuildException(string.Format(Resources.Strings.UTIL0310, ToFile.FullName), e);
            }
        }
    }
}
